package com.nq.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.common.entity.StockList;
import com.nq.dao.stock.StStockMapper;
import com.nq.dao.stock.StStockTickerMapper;
import com.nq.stock.entity.StStock;
import com.nq.stock.entity.StStockTicker;
import com.nq.stock.service.IStStockService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: st_stock
 * @Author: jeecg-boot
 * @Date:   2024-05-30
 * @Version: V1.0
 */
@Service
public class StStockServiceImpl extends ServiceImpl<StStockMapper, StStock> implements IStStockService {

    @Resource
    private StStockMapper stStockMapper;
    @Resource
    private StStockTickerMapper stStockTickerMapper;

    @Override
    @Transactional
    public int addStockList(List<StockList> stockResult) {

        if ( null == stockResult  || stockResult.size() == 0){
            return 0;
        }

        for (StockList stock : stockResult) {
            StStock stStock = new StStock();
            stStock.setCode(stock.getCode());
            stStock.setMarket(stock.getMarket());
            stStock.setContract(stock.getData().getContract());
            stStock.setInterval(stock.getInterval());
            stStock.setName(stock.getData().getName());
            stStock.setVarieties(stock.getData().getVarieties());
            stStock.setCreateTime(new Date());

            // 判断数据库中是否已有该股票
            StStock updateStock = this.getOne(new QueryWrapper<StStock>().eq("code", stock.getCode()));
            if (null != updateStock) {
//                stStockMapper.updateById(updateStock);
                continue;
            }

            stStockMapper.insert(stStock);
        }
        return 1;
    }

}
